package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.dto.LogExcelDto;
import com.c611.smartclassroom.excel.MyExcel;
import com.c611.smartclassroom.model.Log;
import com.c611.smartclassroom.service.LogService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/log")
public class LogController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    LogService logService;

    /**
     * 查询所有日志
     * @return  为日志的一个集合
     */
//    @MyResult
    @PostMapping("/queryLog")
    public DataGridView queryLog(@Param("page") Integer page,@Param("pageSize") Integer pageSize) {
        //1,查询页数是否合理
        if(page < 1 || pageSize < 1) return null;
        //2,查询数据库中的日志表中日志总数
        long logCount = logService.queryLogCount();
        //3,计算该页是否为空(前一页的记录数都大于数据库总数)
        if((page - 1) * pageSize >= logCount) return null;
        //4，参数合理，调用分页查询的方法
        return new DataGridView<Log>(0,"ok",logCount,logService.queryLog(page,pageSize));
    }

    @PostMapping("/addLog")
    public int addLog(@Param("log")Log log) {
        if(log == null) return 0;
        return logService.addLog(log);
    }

    /**
     * 导出日志
     * @param response
     */
    @RequestMapping("/exportLog")
    public void export(HttpServletResponse response){
            //1,查询所有日志
            //2，创建集合对象
            List<Log> logs = logService.queryAllLog();
            List<LogExcelDto> logExcelDtos = new ArrayList<>();

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            for (Log log : logs) {

                LogExcelDto logExcelDto = new LogExcelDto(log.getLogid(),log.getOperator(),log.getOpdescribe(),log.getOpermethod(),log.getOpaddress(),simpleDateFormat.format(log.getOpertime()));
                logExcelDtos.add(logExcelDto);
            }
            //导出操作
            MyExcel.exportExcel(logExcelDtos,"智慧教室系统日志","第一页",LogExcelDto.class,"系统日志.xls",response);
    }

    /**
     * 导入日志，测试时使用
     */
    @RequestMapping("/importLog")
    public void importExcel(){
        String filePath = "D:\\A000.xls";
        //解析excel，
        List<LogExcelDto> personList = MyExcel.importExcel(filePath,1,1,LogExcelDto.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+personList.size()+"】行");

        for (LogExcelDto logExcelDto : personList) {
            Log log = new Log();
            log.setOperator(logExcelDto.getOperator());
            log.setOpdescribe(logExcelDto.getOpdescribe());
            log.setOpermethod(logExcelDto.getOpermethod());
            log.setOpaddress(logExcelDto.getOpaddress());
            try {
                log.setOpertime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(logExcelDto.getOpertime()));
            } catch (ParseException e) {
                System.out.println(e.getMessage() + "日期格式不合里，转换失败！");
                return;
            }
            addLog(log);
        }

        //TODO 保存数据库

    }

}
